package com.andova.manager;

import android.app.Application;

import androidx.annotation.NonNull;

import com.andova.empty.EmptyManager;
import com.andova.empty.api.IEmptyApplicationOption;
import com.andova.empty.api.IEmptyManager;
import com.andova.empty.app.EmptyActivityLifecycleImpl;
import com.andova.lifecycle.ActivityLifecycle;
import com.andova.lifecycle.ActivityLifecycleImpl;
import com.andova.module.ModuleManager;
import com.andova.module.api.IModuleManager;
import com.andova.module.api.IModuleOptions;
import com.andova.module.api.IModuleProvider;
import com.andova.module.app.ModuleActivityLifecycleImpl;

/**
 * Created by Administrator on 2018-03-21.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class AndovaModuleManager {
    private IEmptyManager mEmptyManager;
    private IModuleManager mModuleManager;
    private ActivityLifecycle mLifecycle;
    private static volatile AndovaModuleManager sInstance;

    public static final String ANDOVA_MODULE_BROADCAST_ACTION_MODULE_CHANGED = "andova.module.action.MODULE_CHANGED";

    private AndovaModuleManager() {
        mEmptyManager = new EmptyManager();
        mModuleManager = new ModuleManager();
    }

    public static AndovaModuleManager obj() {
        if (sInstance == null) {
            synchronized (AndovaModuleManager.class) {
                if (sInstance == null) sInstance = new AndovaModuleManager();
            }
        }
        return sInstance;
    }

    public void initialize(@NonNull Application application, IEmptyApplicationOption emptyOption) {
        mLifecycle = new EmptyActivityLifecycleImpl();
        mLifecycle.addLifecycleCallback(mEmptyManager.initialize(emptyOption));
        application.registerActivityLifecycleCallbacks(mLifecycle);
    }

    public void initialize(@NonNull Application application, IModuleOptions options, IModuleProvider provider) {
        mLifecycle = new ModuleActivityLifecycleImpl();
        mLifecycle.addLifecycleCallback(mModuleManager.initialize(application, options, provider));
        application.registerActivityLifecycleCallbacks(mLifecycle);
    }

    public void initialize(@NonNull Application application,
                           IEmptyApplicationOption emptyOption,
                           IModuleOptions options,
                           IModuleProvider provider) {
        mLifecycle = new ActivityLifecycleImpl();
        mLifecycle.addLifecycleCallback(mEmptyManager.initialize(emptyOption));
        mLifecycle.addLifecycleCallback(mModuleManager.initialize(application, options, provider));
        application.registerActivityLifecycleCallbacks(mLifecycle);
    }

    public IEmptyManager empty() {
        return mEmptyManager;
    }

    public IModuleManager module() {
        return mModuleManager;
    }
}
